package cn.wk.org.controller;

import cn.wk.basic.query.DictQuery;
import cn.wk.basic.utils.AjaxResult;
import cn.wk.basic.utils.PageBean;
import cn.wk.org.domain.DictionaryType;
import cn.wk.org.service.IDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionaryType")
public class DictionaryTypeController {
    @Autowired
    private IDictionaryTypeService typeService;

    /**
     * 查询所有信息
     * @return
     */
    @PostMapping
    public PageBean<DictionaryType> findAll(@RequestBody DictQuery dictQuery){
        System.out.println(dictQuery);
        return typeService.queryAll(dictQuery);
    }

    /**
     * 添加或者修改
     * @return 返回一个成功或者失败给前台
     */
    @PutMapping
    public AjaxResult save(@RequestBody DictionaryType dictionaryType){
        try{
            if(dictionaryType.getId() == null){
                typeService.save(dictionaryType);
            }else{
                typeService.update(dictionaryType);
            }
            return new AjaxResult();
        }catch(Exception e){
            e.printStackTrace();
            return new AjaxResult("保存失败");
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable Long id){
        System.out.println(id);
        try{
            typeService.deleteByDetail(id);
            return new AjaxResult();
        }catch(Exception e){
            return new AjaxResult("删除失败");
        }
    }
}
